<?php
class controller_content extends controller_abstract
{
	private $pagesize = 10,$offset = 2;
	function __construct(& $app)
	{
		parent::__construct($app);
		$this->content =  loader::model('content','caitu');
	}
	
	function index()
	{
		$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
		$pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : $this->pagesize;
		$orderby = isset($_GET['orderby']) ? $_GET['orderby']: 'created';
		$db = factory::db();
		$contents = $db->page("SELECT DISTINCT c.contentid,c.*,p.url,p.path FROM #table_content AS c,#table_picture AS p WHERE c.contentid=p.contentid AND c.fetched=1 GROUP BY c.contentid ORDER BY c.{$orderby} DESC", $page, $pagesize);
		$total = $db->get('SELECT COUNT(*) AS total FROM #table_content WHERE fetched=1');
		$this->template->assign(array('total'=> $total['total'],'page'=>$page,'pagesize'=>$pagesize, 'offset'=>$this->offset));
		foreach ($contents as $k => $v)
		{
			$contents[$k]['image'] = $this->system['native_img_url'] ? (UPLOAD_URL.$v['path']):$v['url'];
		}
		$this->template->assign('orderby', $orderby);
		$this->template->assign('content', $contents);
		if($orderby == 'created')
		{
			$this->template->display('show/index.html');
		}
		else 
		{
			$this->template->display('show/hot.html');
		}
	}
	
	function random()
	{
		$db = factory::db();
		$contentids = cache_read('content/contentids.php');
		if(!$contentids)
		{
			$data = $db->select('SELECT contentid FROM #table_content WHERE fetched=1');
			$tmp = array();
			foreach ($data as $v)
			{
				$tmp[] = $v['contentid'];
			}
			$contentids = $tmp;
			cache_write('content/contentids.php', $contentids);
		}
		$keys = array_rand($contentids, 10);
		$ids = array();
		foreach ($keys as $k)
		{
			$ids[] = $contentids[$k];
		}
		$ids = implode(',',$ids);
		$contents = $db->select("SELECT DISTINCT c.contentid,c.*,p.url,p.path FROM #table_content AS c,#table_picture AS p WHERE c.contentid=p.contentid AND c.contentid IN({$ids}) GROUP BY c.contentid");
		foreach ($contents as $k => $v)
		{
			$contents[$k]['image'] = $this->system['native_img_url'] ? (UPLOAD_URL.$v['path']):$v['url'];
		}
		$this->template->assign('content', $contents);
		$this->template->assign('title', '随便看看');
		$this->template->display('show/rand.html');
	}
	
	function content()
	{
		$contentid = $_GET['contentid'];
		$content = $this->content->get($contentid);
		$pictures = loader::model('picture', 'caitu')->get_by_contentid($contentid);
		foreach ($pictures as $k => $v)
		{
			$pictures[$k]['image'] = $this->system['native_img_url'] ? (UPLOAD_URL.$v['path']):$v['url'];
		}
		$this->template->assign($content);
		$this->template->assign('pictures', $pictures);
		$this->template->display('show/content.html');
	}
	
	function tools()
	{
		$this->template->assign('title', '工具箱');
		$this->template->display('show/tools.html');
	}
	
	function test()
	{
		header("Content-type: image/png");
		$string = $_GET['text'];
		$im     = imagecreatefrompng("images/button1.png");
		$orange = imagecolorallocate($im, 220, 210, 60);
		$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
		imagestring($im, 3, $px, 9, $string, $orange);
		imagepng($im);
		imagedestroy($im);

	}
}